{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "采用多元线性,岭回归,SVR,GBDT实现孔隙度回归.ipynb",
      "provenance": [],
      "toc_visible": true,
      "authorship_tag": "ABX9TyPtFnlnuXF3mFMlXvXVu7vC",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/sunyingjian/-Logging-related-network/blob/master/%E9%87%87%E7%94%A8%E5%A4%9A%E5%85%83%E7%BA%BF%E6%80%A7%2C%E5%B2%AD%E5%9B%9E%E5%BD%92%2CSVR%2CGBDT%E5%AE%9E%E7%8E%B0%E5%AD%94%E9%9A%99%E5%BA%A6%E5%9B%9E%E5%BD%92.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Cqy3vXXOMI06",
        "colab_type": "text"
      },
      "source": [
        "### <font size=5px color=\"red\">✦ *Google Colab 突破90分钟自动断开:</font>\n",
        "<p><font size=3px > 每60分钟自动运行代码以刷新90分钟断开限制. 打开 developer-settings (在你的浏览器) 快速健 Ctrl+Shift+I 然后按console 输入以下代码 Enter. ( mac 按 Option+Command+I)</p><b>复制以下隐藏代码粉贴在浏览器console！！不要关闭浏览器以免失效</b>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "3Z8cK8h2Avbv",
        "colab_type": "text"
      },
      "source": [
        "<code>function ClickConnect(){\n",
        "console.log(\"Working\"); \n",
        "document.querySelector(\"colab-connect-button\").click() \n",
        "}setInterval(ClickConnect,6000)</code>"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "jtClEMAMLVHw",
        "colab_type": "code",
        "cellView": "both",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "outputId": "0a1609b0-136d-4172-afac-5c6fa4de2bed"
      },
      "source": [
        "#@markdown <h3>← 输入了代码后运行以防止断开</h>\n",
        "\n",
        "\n",
        "import IPython\n",
        "from google.colab import output\n",
        "\n",
        "display(IPython.display.Javascript('''\n",
        " function ClickConnect(){\n",
        "   btn = document.querySelector(\"colab-connect-button\")\n",
        "   if (btn != null){\n",
        "     console.log(\"Click colab-connect-button\"); \n",
        "     btn.click() \n",
        "     }\n",
        "   \n",
        "   btn = document.getElementById('ok')\n",
        "   if (btn != null){\n",
        "     console.log(\"Click reconnect\"); \n",
        "     btn.click() \n",
        "     }\n",
        "  }\n",
        "  \n",
        "setInterval(ClickConnect,60000)\n",
        "'''))\n",
        "\n",
        "print(\"Done.\")"
      ],
      "execution_count": 1,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "application/javascript": [
              "\n",
              " function ClickConnect(){\n",
              "   btn = document.querySelector(\"colab-connect-button\")\n",
              "   if (btn != null){\n",
              "     console.log(\"Click colab-connect-button\"); \n",
              "     btn.click() \n",
              "     }\n",
              "   \n",
              "   btn = document.getElementById('ok')\n",
              "   if (btn != null){\n",
              "     console.log(\"Click reconnect\"); \n",
              "     btn.click() \n",
              "     }\n",
              "  }\n",
              "  \n",
              "setInterval(ClickConnect,60000)\n"
            ],
            "text/plain": [
              "<IPython.core.display.Javascript object>"
            ]
          },
          "metadata": {
            "tags": []
          }
        },
        {
          "output_type": "stream",
          "text": [
            "Done.\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "oz9RNIyQabNm",
        "colab_type": "text"
      },
      "source": [
        "#引入数据"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "9kPj_TVcaYLl",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 138
        },
        "outputId": "ae5af5b8-48ae-4de8-9d6b-cfa809063669"
      },
      "source": [
        "! git clone https://github.com/sunyingjian/numpy-.git"
      ],
      "execution_count": 2,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Cloning into 'numpy-'...\n",
            "remote: Enumerating objects: 17, done.\u001b[K\n",
            "remote: Counting objects:   5% (1/17)\u001b[K\rremote: Counting objects:  11% (2/17)\u001b[K\rremote: Counting objects:  17% (3/17)\u001b[K\rremote: Counting objects:  23% (4/17)\u001b[K\rremote: Counting objects:  29% (5/17)\u001b[K\rremote: Counting objects:  35% (6/17)\u001b[K\rremote: Counting objects:  41% (7/17)\u001b[K\rremote: Counting objects:  47% (8/17)\u001b[K\rremote: Counting objects:  52% (9/17)\u001b[K\rremote: Counting objects:  58% (10/17)\u001b[K\rremote: Counting objects:  64% (11/17)\u001b[K\rremote: Counting objects:  70% (12/17)\u001b[K\rremote: Counting objects:  76% (13/17)\u001b[K\rremote: Counting objects:  82% (14/17)\u001b[K\rremote: Counting objects:  88% (15/17)\u001b[K\rremote: Counting objects:  94% (16/17)\u001b[K\rremote: Counting objects: 100% (17/17)\u001b[K\rremote: Counting objects: 100% (17/17), done.\u001b[K\n",
            "remote: Compressing objects: 100% (17/17), done.\u001b[K\n",
            "remote: Total 106 (delta 6), reused 0 (delta 0), pack-reused 89\u001b[K\n",
            "Receiving objects: 100% (106/106), 3.90 MiB | 21.58 MiB/s, done.\n",
            "Resolving deltas: 100% (39/39), done.\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Iiv8bPYRNGFF",
        "colab_type": "text"
      },
      "source": [
        "#数据处理"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "yzNIVAPkM8P9",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "outputId": "07405ba3-a833-4a8a-a03e-c54b34b4924e"
      },
      "source": [
        "%matplotlib inline\n",
        "#%matplotlib inline 可以在Ipython编译器里直接使用，功能是可以内嵌绘图，并且可以省略掉plt.show()这一步。\n",
        "import pandas as pd\n",
        "import numpy as np\n",
        "import matplotlib as mpl\n",
        "import matplotlib.pyplot as plt\n",
        "import matplotlib.colors as colors\n",
        "from mpl_toolkits.axes_grid1 import make_axes_locatable\n",
        "from pandas import set_option\n",
        "set_option(\"display.max_rows\", 10)#设置要显示的默认行数，显示的最大行数是10\n",
        "pd.options.mode.chained_assignment = None #为了在增加列表行数的时候防止出现setting with copy warning\n",
        "data = pd.read_csv(\"/content/numpy-/预测核磁共振和孔隙度测井数据集/WR313H_logs.csv\")\n",
        "data=data.dropna(how='any')\n",
        "data.shape"
      ],
      "execution_count": 3,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(6540, 5)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 3
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "w6CtSWNbPRpe",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 288
        },
        "outputId": "59254fa3-08e3-401e-a26e-896f0209451e"
      },
      "source": [
        "porosity_labels = data['Porosity'].values\n",
        "porosity_data = data.drop([ 'Depth','Porosity'], axis=1)\n",
        "porosity_data.describe()"
      ],
      "execution_count": 4,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>GR</th>\n",
              "      <th>Caliper</th>\n",
              "      <th>Ring_resistivity</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>count</th>\n",
              "      <td>6540.000000</td>\n",
              "      <td>6540.000000</td>\n",
              "      <td>6540.000000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>mean</th>\n",
              "      <td>100.697470</td>\n",
              "      <td>21.794128</td>\n",
              "      <td>2.631533</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>std</th>\n",
              "      <td>10.971742</td>\n",
              "      <td>0.600965</td>\n",
              "      <td>15.522354</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>min</th>\n",
              "      <td>35.249556</td>\n",
              "      <td>21.045932</td>\n",
              "      <td>0.330400</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>25%</th>\n",
              "      <td>94.774698</td>\n",
              "      <td>21.484780</td>\n",
              "      <td>1.582150</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>50%</th>\n",
              "      <td>102.349457</td>\n",
              "      <td>21.698458</td>\n",
              "      <td>1.848500</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>75%</th>\n",
              "      <td>108.030412</td>\n",
              "      <td>21.941599</td>\n",
              "      <td>2.095000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>max</th>\n",
              "      <td>126.619332</td>\n",
              "      <td>30.664150</td>\n",
              "      <td>649.611600</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "                GR      Caliper  Ring_resistivity\n",
              "count  6540.000000  6540.000000       6540.000000\n",
              "mean    100.697470    21.794128          2.631533\n",
              "std      10.971742     0.600965         15.522354\n",
              "min      35.249556    21.045932          0.330400\n",
              "25%      94.774698    21.484780          1.582150\n",
              "50%     102.349457    21.698458          1.848500\n",
              "75%     108.030412    21.941599          2.095000\n",
              "max     126.619332    30.664150        649.611600"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 4
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "3W9N3ArWQEEW",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "outputId": "6af0cab6-35fa-4231-9fd3-02e4d8467efc"
      },
      "source": [
        "from sklearn.model_selection import train_test_split\n",
        "\n",
        "X_train, X_test, y_train, y_test = train_test_split(\n",
        "        porosity_data, porosity_labels, test_size=0.2, random_state=42)\n",
        "X_train.shape"
      ],
      "execution_count": 5,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(5232, 3)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 5
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "wKeT4bxSQZrl",
        "colab_type": "text"
      },
      "source": [
        "#先看下相关性"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "gMA2IFJXRrRb",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 52
        },
        "outputId": "daa80ad5-d6e7-4d2e-e08f-55fc9addfdb4"
      },
      "source": [
        "from pandas import set_option\n",
        "import seaborn as sns\n",
        "from sklearn import  model_selection\n",
        "import statsmodels.api as sm\n",
        "import statsmodels.formula.api as smf"
      ],
      "execution_count": 6,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "/usr/local/lib/python3.6/dist-packages/statsmodels/tools/_testing.py:19: FutureWarning: pandas.util.testing is deprecated. Use the functions in the public API at pandas.testing instead.\n",
            "  import pandas.util.testing as tm\n"
          ],
          "name": "stderr"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "XMaEfwVgRaOc",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 121
        },
        "outputId": "a6386fa9-9d8a-4564-c14d-ea6bb9c13ade"
      },
      "source": [
        "model = smf.ols(formula='Porosity~GR+Caliper+Ring_resistivity',data=data).fit()\n",
        "print(f'模型的偏回归系数为{model.params}\\n')"
      ],
      "execution_count": 7,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "模型的偏回归系数为Intercept          -0.201605\n",
            "GR                  0.000212\n",
            "Caliper             0.025088\n",
            "Ring_resistivity   -0.000068\n",
            "dtype: float64\n",
            "\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "1MpS95V1QYmb",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "outputId": "49cc781a-9c7d-4ffb-aeb9-69cc37807bb9"
      },
      "source": [
        "#计算建模中的因变量均值\n",
        "ybar = data.Porosity.mean()\n",
        "#统计变量个数与观测个数\n",
        "p=model.df_model\n",
        "n=data.shape[0]\n",
        "##计算回归离差平方和\n",
        "RSS=np.sum((model.fittedvalues-ybar)**2)\n",
        "RSS"
      ],
      "execution_count": 8,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "1.531428215533501"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 8
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "jt0nHS39SeC6",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "outputId": "d2438835-0a15-47b2-cffb-aac30b5d79cf"
      },
      "source": [
        "#计算误差平方和\n",
        "ESS = np.sum(model.resid**2)\n",
        "ESS"
      ],
      "execution_count": 9,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "38.46811521714825"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 9
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "cT_2tdCrSgvB",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "outputId": "bfc0747c-a142-4065-c112-95f97d1e8c03"
      },
      "source": [
        "#计算F值\n",
        "F = (RSS/p)/(ESS/(n-p-1))\n",
        "F"
      ],
      "execution_count": 10,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "86.73343070596627"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 10
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Gr_QvgTESjlG",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 520
        },
        "outputId": "c9a07801-5dbf-4565-99e1-0c243dbb280a"
      },
      "source": [
        "model.summary()"
      ],
      "execution_count": 11,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<table class=\"simpletable\">\n",
              "<caption>OLS Regression Results</caption>\n",
              "<tr>\n",
              "  <th>Dep. Variable:</th>        <td>Porosity</td>     <th>  R-squared:         </th>  <td>   0.038</td> \n",
              "</tr>\n",
              "<tr>\n",
              "  <th>Model:</th>                   <td>OLS</td>       <th>  Adj. R-squared:    </th>  <td>   0.038</td> \n",
              "</tr>\n",
              "<tr>\n",
              "  <th>Method:</th>             <td>Least Squares</td>  <th>  F-statistic:       </th>  <td>   86.73</td> \n",
              "</tr>\n",
              "<tr>\n",
              "  <th>Date:</th>             <td>Sun, 02 Aug 2020</td> <th>  Prob (F-statistic):</th>  <td>4.97e-55</td> \n",
              "</tr>\n",
              "<tr>\n",
              "  <th>Time:</th>                 <td>14:13:10</td>     <th>  Log-Likelihood:    </th>  <td>  7514.4</td> \n",
              "</tr>\n",
              "<tr>\n",
              "  <th>No. Observations:</th>      <td>  6540</td>      <th>  AIC:               </th> <td>-1.502e+04</td>\n",
              "</tr>\n",
              "<tr>\n",
              "  <th>Df Residuals:</th>          <td>  6536</td>      <th>  BIC:               </th> <td>-1.499e+04</td>\n",
              "</tr>\n",
              "<tr>\n",
              "  <th>Df Model:</th>              <td>     3</td>      <th>                     </th>      <td> </td>    \n",
              "</tr>\n",
              "<tr>\n",
              "  <th>Covariance Type:</th>      <td>nonrobust</td>    <th>                     </th>      <td> </td>    \n",
              "</tr>\n",
              "</table>\n",
              "<table class=\"simpletable\">\n",
              "<tr>\n",
              "          <td></td>            <th>coef</th>     <th>std err</th>      <th>t</th>      <th>P>|t|</th>  <th>[0.025</th>    <th>0.975]</th>  \n",
              "</tr>\n",
              "<tr>\n",
              "  <th>Intercept</th>        <td>   -0.2016</td> <td>    0.036</td> <td>   -5.640</td> <td> 0.000</td> <td>   -0.272</td> <td>   -0.132</td>\n",
              "</tr>\n",
              "<tr>\n",
              "  <th>GR</th>               <td>    0.0002</td> <td> 8.84e-05</td> <td>    2.396</td> <td> 0.017</td> <td> 3.85e-05</td> <td>    0.000</td>\n",
              "</tr>\n",
              "<tr>\n",
              "  <th>Caliper</th>          <td>    0.0251</td> <td>    0.002</td> <td>   15.887</td> <td> 0.000</td> <td>    0.022</td> <td>    0.028</td>\n",
              "</tr>\n",
              "<tr>\n",
              "  <th>Ring_resistivity</th> <td>-6.774e-05</td> <td> 6.25e-05</td> <td>   -1.084</td> <td> 0.279</td> <td>   -0.000</td> <td> 5.48e-05</td>\n",
              "</tr>\n",
              "</table>\n",
              "<table class=\"simpletable\">\n",
              "<tr>\n",
              "  <th>Omnibus:</th>       <td>3780.366</td> <th>  Durbin-Watson:     </th> <td>   0.018</td> \n",
              "</tr>\n",
              "<tr>\n",
              "  <th>Prob(Omnibus):</th>  <td> 0.000</td>  <th>  Jarque-Bera (JB):  </th> <td>40865.082</td>\n",
              "</tr>\n",
              "<tr>\n",
              "  <th>Skew:</th>           <td> 2.601</td>  <th>  Prob(JB):          </th> <td>    0.00</td> \n",
              "</tr>\n",
              "<tr>\n",
              "  <th>Kurtosis:</th>       <td>14.086</td>  <th>  Cond. No.          </th> <td>3.91e+03</td> \n",
              "</tr>\n",
              "</table><br/><br/>Warnings:<br/>[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.<br/>[2] The condition number is large, 3.91e+03. This might indicate that there are<br/>strong multicollinearity or other numerical problems."
            ],
            "text/plain": [
              "<class 'statsmodels.iolib.summary.Summary'>\n",
              "\"\"\"\n",
              "                            OLS Regression Results                            \n",
              "==============================================================================\n",
              "Dep. Variable:               Porosity   R-squared:                       0.038\n",
              "Model:                            OLS   Adj. R-squared:                  0.038\n",
              "Method:                 Least Squares   F-statistic:                     86.73\n",
              "Date:                Sun, 02 Aug 2020   Prob (F-statistic):           4.97e-55\n",
              "Time:                        14:13:10   Log-Likelihood:                 7514.4\n",
              "No. Observations:                6540   AIC:                        -1.502e+04\n",
              "Df Residuals:                    6536   BIC:                        -1.499e+04\n",
              "Df Model:                           3                                         \n",
              "Covariance Type:            nonrobust                                         \n",
              "====================================================================================\n",
              "                       coef    std err          t      P>|t|      [0.025      0.975]\n",
              "------------------------------------------------------------------------------------\n",
              "Intercept           -0.2016      0.036     -5.640      0.000      -0.272      -0.132\n",
              "GR                   0.0002   8.84e-05      2.396      0.017    3.85e-05       0.000\n",
              "Caliper              0.0251      0.002     15.887      0.000       0.022       0.028\n",
              "Ring_resistivity -6.774e-05   6.25e-05     -1.084      0.279      -0.000    5.48e-05\n",
              "==============================================================================\n",
              "Omnibus:                     3780.366   Durbin-Watson:                   0.018\n",
              "Prob(Omnibus):                  0.000   Jarque-Bera (JB):            40865.082\n",
              "Skew:                           2.601   Prob(JB):                         0.00\n",
              "Kurtosis:                      14.086   Cond. No.                     3.91e+03\n",
              "==============================================================================\n",
              "\n",
              "Warnings:\n",
              "[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n",
              "[2] The condition number is large, 3.91e+03. This might indicate that there are\n",
              "strong multicollinearity or other numerical problems.\n",
              "\"\"\""
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 11
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "WQJ5d3J5StTq",
        "colab_type": "text"
      },
      "source": [
        "#线性回归"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "tJsZHqhch7GC",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 52
        },
        "outputId": "2d643596-2466-4de6-cbb2-a6de9a5caf9f"
      },
      "source": [
        "import sklearn\n",
        "from sklearn import metrics\n",
        "from sklearn import linear_model\n",
        "import statsmodels.api as sm\n",
        "from sklearn.metrics import r2_score\n",
        "\n",
        "reg = linear_model.LinearRegression()\n",
        "reg.fit(X_train,y_train)\n",
        "y_pred = reg.predict(X_test)\n",
        "print(f'MSE={metrics.mean_absolute_error(y_test,y_pred)}')\n",
        "print(f'可决系数R2:{r2_score(y_test,y_pred)}')"
      ],
      "execution_count": 28,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "MSE=0.052455566216975204\n",
            "可决系数R2:0.04585913996340052\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Wv4SoYqxh2bN",
        "colab_type": "text"
      },
      "source": [
        "##岭回归"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "brLEZvKNSsu0",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 52
        },
        "outputId": "1f4c6949-7b04-434f-870a-31a6fb851c63"
      },
      "source": [
        "reg1 = linear_model.Ridge(alpha=0.5)\n",
        "reg1.fit(X_train,y_train)\n",
        "y_pred = reg1.predict(X_test)\n",
        "print('Mean squared error: %f'\n",
        "      % metrics.mean_squared_error(y_test, y_pred))\n",
        "print(f'可决系数R2:{r2_score(y_test,y_pred)}')"
      ],
      "execution_count": 29,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Mean squared error: 0.006704\n",
            "可决系数R2:0.04585351695210571\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "xsMHDuqgkFv_",
        "colab_type": "text"
      },
      "source": [
        "#SVR支持向量回归"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "SbOq9Rkvj_OM",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 52
        },
        "outputId": "629aa675-9bad-4bfe-a793-3e0d5d316a83"
      },
      "source": [
        "from sklearn import svm\n",
        "clf2 = svm.SVR()\n",
        "clf2.fit(X_train,y_train)\n",
        "y_pred = clf2.predict(X_test)\n",
        "print('Mean squared error: %f'\n",
        "      % metrics.mean_squared_error(y_test, y_pred))\n",
        "print(f'可决系数R2:{r2_score(y_test,y_pred)}')"
      ],
      "execution_count": 30,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Mean squared error: 0.006114\n",
            "可决系数R2:0.1297301490962368\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "VhWyWOoEk2Of",
        "colab_type": "text"
      },
      "source": [
        "#GBDT回归"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "A5ql8pROk45K",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 52
        },
        "outputId": "53b9adcf-f6e7-4753-f19c-3e4058643710"
      },
      "source": [
        "from sklearn.ensemble import GradientBoostingRegressor\n",
        "est = GradientBoostingRegressor(n_estimators=100,learning_rate=0.1,max_depth=1,random_state=0,\n",
        "                                loss='ls').fit(X_train,y_train)\n",
        "y_pred = est.predict(X_test)\n",
        "print('Mean squared error: %f'\n",
        "      % metrics.mean_squared_error(y_test, y_pred))\n",
        "print(f'可决系数R2:{r2_score(y_test,y_pred)}')"
      ],
      "execution_count": 31,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Mean squared error: 0.002324\n",
            "可决系数R2:0.6692259002343055\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "bro3bh0QmlUM",
        "colab_type": "text"
      },
      "source": [
        "#画图"
      ]
    }
  ]
}
